package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * @Project: reggie_order_out
 * @Author: 29292
 * @Create: 2022-05-16 16:48
 * @Desc：后台员工登录
 **/
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService es;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable Long id){
        log.info("【查询ID】{}",id);
        Employee byId = es.getById(id);
        return R.success(byId);
    }

    @PutMapping
    public R updStatus(HttpServletRequest request,@RequestBody Employee employee){
        log.info("【员工账户修改/禁用】==>{}",employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) request.getSession().getAttribute("employeeId"));
        boolean flag = es.updateById(employee);
        return flag ? R.success("员工账号修改/禁用成功!") : R.error("员工账户修改/禁用时异常!");
    }

    //?page=1&pageSize=10&name=1
    @GetMapping("/page")
    public R getPage(Integer page,Integer pageSize,String name){
        IPage<Employee> ipage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName,name);
        es.page(ipage, lqw);
        return R.success(ipage);
    }

    @PostMapping
    public R<String> insert(@RequestBody Employee employee, HttpServletRequest request){
        log.info("接收请求体参数【employee】==>{}",employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long employeeId = (Long)request.getSession().getAttribute("employeeId");
        employee.setCreateUser(employeeId);
        employee.setUpdateUser(employeeId);
        log.info("保存前数据打印==>{}",employee);
        boolean flag = es.save(employee);

        return R.success(flag ? "新增成功" : "新增失败");

    }

    @PostMapping("/login")
    public R<Employee> employeeLogin(@RequestBody Employee employee, HttpServletRequest request){
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        System.out.println(employee);
        System.out.println(employee.getUsername());
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee result = es.getOne(lqw);

        String md5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (result == null){
            //未查询到信息则表示无账户信息
            return R.error("账户不存在!");
        }

        if (!result.getPassword().equals(md5Password)){
            //如果密码匹配不成功则表明密码不正确
            return R.error("密码不正确!");
        }
        //判断此账户状态是否被禁用
        if (result.getStatus() == 0){
            return R.error("此账户已被禁用!");
        }

        request.getSession().setAttribute("employeeId", result.getId());
        return R.success(result);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        request.getSession().removeAttribute("employeeId");
        return R.success("已退出!");
    }
}
